﻿/* 
 *  <copyright file="PlatformerScreen.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using SEGameExample.Kludge;
using SobrietyEngine.Actor;
using SobrietyEngine.Components;
using SobrietyEngine.Components.UDMF;
using SobrietyEngine.Guids;
using SobrietyEngine.Logging;
using SobrietyEngine.Scene;
using SobrietyEngine.Screens;
using SobrietyEngine.UDMF;
using SEGameExample.InputMappers;
using SobrietyEngine.Actor.Builders.UDMF;

namespace SEGameExample.Screens
{
    class FPSUDMF: BaseLevel 
    {
        public FPSUDMF()
            : base("FPSUDMF")
        {
        }

        public override void Initialize()
        {
            base.Initialize();

            //set some options
            //turn on logging of some events
            EventRouter.LogRaisedEvents = true;
            EventRouter.LogEventIgnore.Add("Input.");
            EventRouter.LogEventIgnore.Add(".Move");
            EventRouter.LogEventIgnore.Add(".Rotate");
            EventRouter.LogEventIgnore.Add(".ActorInit");

            //set some logging options
            Logger.AppendLoggingType(Logger.LogType.AssertOnError |
                                     Logger.LogType.VSConsole |
                                     Logger.LogType.RaiseLogEvent);

            //set up our PlayerOne (first found user with an Xbox 360 controller)
            //turn off any controllers that may be active (shoudl always disable controllers when you are finished!)
            PlayerOne.DisableAllInputMappers();
            if(!PlayerOne.HasInputMapper(typeof(PlatformerEventMapper)))
                PlayerOne.AddInputMapper(new PlatformerEventMapper(EventRouter, PlayerOne.Guid));
            PlayerOne.EnableInputMapper(typeof(PlatformerEventMapper));

            //forward declaration of some variables used in here
            IActor actor = null;
            IScene scene = SceneGameService.Scenes[SceneName];

            //test loading UDMF formatted actor tempaltes
            actorGameService.LoadActorTemplates("actors/actorDefs");

            //testing this, since this is how builders will eb accessed via script
            //UDMFMapBldr bldr = ActorFactory.GetBuilder<UDMFMapBldr>(typeof(UDMFMapBldr).ToString());            

            IActor map = UDMFMapBldr.Build(Scene, /*"Scenes/testUDMF"*/ "Scenes/HoleInSector", 
                new Vector3(0, 0, 0), Vector3.Forward, new Vector3(0.07f, 0.07f, 0.07f));

            //lets try getting the player start pos from the actors name using guids
            //this is assuming there is an actor out there named 'playerstart'
            CGuid playerStart = CGuid.GetGuid("playerstart");
            IActor startAct = playerStart.Resolve<IActor>();
            IPosition startPosCmp = startAct.Get<IPosition>();
            Vector2 startPos = startPosCmp.PositionV2;

            //Test some gui and hud stuff
            //create a 2D gui camera     
            //IActor cam2D = GUISpawner.Camera2D(Scene, ScreenManager.FullScreenViewport);
            IActor cam2d = SobrietyEngine.Actor.Builders.Cameras.GuiCameraBldr.Build(Scene, ScreenManager.FullScreenViewport);

            //some help text about contros
            String help = "DoomBuilder UDMF with FPSCam. \n ";
            help += "Free move cam, no clipping";
            help += "Left thumbstick moves\n";
            help += "Right thumbstick looks\n";
            help += "(B) exits, (Y) toggles physics debug \n";
            help += "(LB) toggle debug console\n";

            SobrietyEngine.Actor.Builders.GUI.Text2D.Build(Scene, new Vector2(0, ScreenManager.FullScreenViewport.Height / 2), help);

            //fps cam world axis. THe doom map is built so that X,Y are right/forward and Z is up
            Matrix trans = Matrix.CreateFromYawPitchRoll(0f, (float)(Math.PI/2), 0f);
            Vector3 up = Vector3.Transform(Vector3.Up, trans) ;
            Vector3 right = Vector3.Transform(Vector3.Right, trans);
            Vector3 lookat = Vector3.Transform(Vector3.Forward, trans);

                       
            actor = SobrietyEngine.Actor.Builders.Cameras.FPSCameraBldr.Build(scene, ScreenManager.FullScreenViewport, ScreenManager.GraphicsDevice, PlayerOne.Guid, up, right, lookat);                                    
            //actor = SobrietyEngine.Actor.Builders.Cameras.FreeFlyCameraBldr.Build(scene, ScreenManager.FullScreenViewport, ScreenManager.GraphicsDevice, PlayerOne.Guid);            
            actor.Get<IPosition>().PositionV3 = new Vector3(startPos, 2f);

            //turn on our controllers
            SceneGameService.Scenes[SceneName].EnableControllers();
        }

    }
}